<?php
require('../../../setting.php');
$idcard = $_POST['idcard'];
$name = $_POST['name'];
$userid = $_POST['userid'];
if($idcard == '' || $name == ''){
    echo "<script>alert('请输入身份证号和姓名');location='../';</script>";
}
require('../../../cofd/common.php');
$secretId = $api_authcode_human['api_authcode_human_secret'];
$secretKey = $api_authcode_human['api_authcode_human_key'];
$source = 'market';
$datetime = gmdate('D, d M Y H:i:s T');
$signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
$sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
$auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign);
$method = 'GET';
$headers = array(
    'X-Source' => $source,
    'X-Date' => $datetime,
    'Authorization' => $auth,
    
);
$queryParams = array (
    'idcard' => $idcard,
    'name' => $name,
);
$bodyParams = array (

);
$url = $api_authcode_human['api_authcode_human_url'];
if (count($queryParams) > 0) {
    $url .= '?' . http_build_query($queryParams);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) {
    return $k . ': ' . $v;
}, array_values($headers), array_keys($headers)));
if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));
}

$data = curl_exec($ch);
if (curl_errno($ch)) {
    echo "Error: " . curl_error($ch);
} else {
    $data2 = json_decode($data, true);
    if($data2['code'] == 0 ){
        $sql = "update exam_user set name=?,smrz=? where id=?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param('sss', $name, $idcard, $userid);
        if($stmt->execute()){
            $_SESSION['exam_smrz'] = true;
            echo "<script>alert('实名认证成功');location='../realhumantoauth.php';</script>";  
        }else{
            echo "<script>alert('实名认证失败');location='../realhumantoauth.php';</script>";
        }
    }else{
        echo "<script>alert('实名认证失败');location='../realhumantoauth.php';</script>";
    }
}
curl_close($ch);